// pages/authorization/authorization.js
const app = getApp();
const http = app.http;
const userInfoManager = app.userInfoManager;
const mpAppInfo = app.mpAppInfo;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    color: '#ffffff',
    background: '#24b569',
    routePath: '',
    isSelectedAgreement: false,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    // this.routePath = options.routePath;
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {},
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  },
  onClickTick() {

    this.setData({
      isSelectedAgreement: !this.data.isSelectedAgreement
    })
  },
  onClickAgreement() {
    wx.navigateTo({
      url: '/pages/login-process/agreement/agreement',
      success: (result) => {

      },
      fail: () => {},
      complete: () => {}
    });

  },
  onClickAuth() {
    if (this.data.isSelectedAgreement) {
      return;
    }
    wx.showToast({
      title: '请先阅读并勾选我们的协议噢',
      icon: 'none',
      image: '',
      duration: 1500,
      mask: false,
      success: (result) => {

      },
      fail: () => {},
      complete: () => {}
    });

  },
  getUserInfo(e) {
    const msg = e.detail.errMsg;
    if (msg !== "getUserInfo:ok") {
      wx.showToast({
        title: '只有授权我们才能提供更多的服务哦',
        icon: 'none',
        image: '',
        duration: 1500,
        mask: false,
        success: (result) => {

        },
        fail: () => {},
        complete: () => {}
      });
      return
    }

    if (!e.detail.rawData || !e.detail.signature || !e.detail.encryptedData || !e.detail.iv) {
      wx.showToast({
        title: '授权用户信息失败',
        icon: 'none',
        image: '',
        duration: 1500,
        mask: false,
        success: (result) => {

        },
        fail: () => {},
        complete: () => {}
      });
      return;
    }
    app.globalData.wechatUserInfo = e.detail;
    app.loginProgressManager.needAuth = false;
    wx.showToast({
      title: '授权成功',
      icon: 'none',
      image: '',
      duration: 1500,
      mask: false,
      success: (result) => {

      },
      fail: () => {},
      complete: () => {}
    });
    wx.reLaunch({
      url: '/pages/login-process/login/login',
      success: (result) => {

      },
      fail: () => {},
      complete: () => {}
    });
  }
})